﻿using MengLin.OOP.DesignPattern.IteratorPattern.BuyTicket.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MengLin.OOP.DesignPattern.IteratorPattern.BuyTicket.Implements
{
    /// <summary>
    /// 具体的迭代器
    /// </summary>
    public class ConcreteIterator : AbstractIterator
    {
        private ConcreteAggregate _aggregate;
        private int currentIndex = 0;

        /// <summary>
        /// Ctor
        /// </summary>
        public ConcreteIterator(ConcreteAggregate aggregate)
        {
            _aggregate = aggregate;
        }

        /// <summary>
        /// 得到集合的第一个元素
        /// </summary>
        /// <returns></returns>
        public override object First()
        {
            return _aggregate[0];
        }

        /// <summary>
        /// 得到集合的当前元素
        /// </summary>
        /// <returns></returns>
        public override object CurretItem()
        {
            return _aggregate[currentIndex];
        }

        /// <summary>
        /// 得到集合的最后一个元素
        /// </summary>
        /// <returns></returns>
        public override object Last()
        {
            return _aggregate[_aggregate.Count - 1];
        }

        /// <summary>
        /// 得到集合的下一个元素
        /// </summary>
        /// <returns></returns>
        public override object Next()
        {
            object obj = null;
            currentIndex++;
            if (currentIndex < _aggregate.Count)
            {
                obj = _aggregate[currentIndex];
            }
            return obj;
        }

        /// <summary>
        /// 得到集合的上一个元素
        /// </summary>
        /// <returns></returns>
        public override object Previous()
        {
            object obj = null;
            currentIndex--;
            if (currentIndex > 0)
            {
                obj = _aggregate[currentIndex];
            }
            return obj;
        }

        /// <summary>
        /// 是否完成
        /// </summary>
        /// <returns></returns>
        public override bool IsDone()
        {
            return currentIndex >= _aggregate.Count ? true : false;
        }
    }
}
